package org.dromara.pe.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.dromara.basic.service.IDictService;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.pe.domain.DeviceApi;
import org.dromara.pe.domain.vo.*;
import org.dromara.pe.mapper.DeviceApiMapper;
import org.dromara.pe.service.IDeviceApiService;
import org.springframework.stereotype.Service;
import org.dromara.pe.domain.bo.ScopeSettingBo;
import org.dromara.pe.domain.ScopeSetting;
import org.dromara.pe.mapper.ScopeSettingMapper;
import org.dromara.pe.service.IScopeSettingService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 体测范围设置Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@RequiredArgsConstructor
@Service
public class ScopeSettingServiceImpl implements IScopeSettingService {

    private final ScopeSettingMapper baseMapper;

    private final IDictService dictService;

    /**
     * 查询体测范围设置
     */
    @Override
    public ScopeSettingVo queryById(Long scopeId){
        ScopeSettingBo scopeSettingBo=new ScopeSettingBo();
        scopeSettingBo.setScopeId(scopeId);
        QueryWrapper<ScopeSetting> lqw = buildQueryWrapper(scopeSettingBo);
        ScopeSettingVo scopeSettingVo = baseMapper.selectScopeSettingVo(lqw);
        String sportId = scopeSettingVo.getSportId();
        if(StringUtils.isNotEmpty(sportId)){
            LoginUser loginUser = LoginHelper.getLoginUser();
            String peProjectName = dictService.getDictName("pe_project_name", sportId,loginUser.getTenantId());
            scopeSettingVo.setSportName(peProjectName);
        }
        return scopeSettingVo;
    }

    /**
     * 查询体测范围设置列表
     */
    @Override
    public TableDataInfo<ScopeSettingVo> queryPageList(ScopeSettingBo bo, PageQuery pageQuery) {
        QueryWrapper<ScopeSetting> lqw = buildQueryWrapper(bo);
        Page<ScopeSettingVo> result = baseMapper.selectScopeSettingList(pageQuery.build(), lqw);
        this.setDate(result.getRecords());
        return TableDataInfo.build(result);
    }

    /**
     * 查询体测范围设置列表
     */
    @Override
    public List<ScopeSettingVo> queryList(ScopeSettingBo bo) {
        QueryWrapper<ScopeSetting> lqw = buildQueryWrapper(bo);
        List<ScopeSettingVo> scopeSettingVos = baseMapper.selectScopeSettingList(lqw);
        this.setDate(scopeSettingVos);
        return scopeSettingVos;
    }
    private void setDate(List<ScopeSettingVo> scopeSettingVos){
        for(ScopeSettingVo scopeSettingVo:scopeSettingVos){
            String sportId = scopeSettingVo.getSportId();
            if(StringUtils.isNotEmpty(sportId)){
                LoginUser loginUser = LoginHelper.getLoginUser();
                String peProjectName = dictService.getDictName("pe_project_name", sportId,loginUser.getTenantId());
                scopeSettingVo.setSportName(peProjectName);
            }
        }
    }

    private QueryWrapper<ScopeSetting> buildQueryWrapper(ScopeSettingBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<ScopeSetting> lqw = new QueryWrapper<>();
        lqw.eq(bo.getScopeId() != null, "ppss.scope_id", bo.getScopeId());
        lqw.eq(bo.getIsDevice() != null, "ppss.is_device", bo.getIsDevice());
        lqw.eq(bo.getIsStandard() != null, "ppss.is_standard", bo.getIsStandard());
        lqw.eq(StringUtils.isNotBlank(bo.getSportId()), "ppss.sport_id", bo.getSportId());
        lqw.in(CollectionUtils.isNotEmpty(bo.getScopeIds()), "ppss.scope_id", bo.getScopeIds());
        lqw.eq(bo.getAreaId() != null, "ppss.area_id", bo.getAreaId());
        lqw.like(StringUtils.isNotBlank(bo.getAreaName()), "ppa.area_name", bo.getAreaName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlaceType()), "ppss.place_type", bo.getPlaceType());
        lqw.like(StringUtils.isNotBlank(bo.getScopeTypeName()), "ppst.scope_type_name", bo.getScopeTypeName());
        lqw.eq(bo.getTeacherId() != null, "ppss.teacher_id", bo.getTeacherId());
        lqw.like(StringUtils.isNotBlank(bo.getTeacherName()), "su.nick_name", bo.getTeacherName());
        lqw.eq(StringUtils.isNotBlank(bo.getScopeType()), "ppss.scope_type", bo.getScopeType());
        lqw.eq(StringUtils.isNotBlank(bo.getIsEnter()), "ppss.is_enter", bo.getIsEnter());
        lqw.orderByAsc("ppss.sport_id");
        return lqw;
    }

    /**
     * 新增体测范围设置
     */
    @Override
    public Boolean insertByBo(ScopeSettingBo bo) {
        ScopeSetting add = MapstructUtils.convert(bo, ScopeSetting.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setScopeId(add.getScopeId());
        }
        return flag;
    }

    /**
     * 修改体测范围设置
     */
    @Override
    public Boolean updateByBo(ScopeSettingBo bo) {
        ScopeSetting update = MapstructUtils.convert(bo, ScopeSetting.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ScopeSetting entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除体测范围设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
